﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using VietKinsNutrition.Data;
using System.Data;
using System.Windows.Forms;

namespace VietKinsNutrition.Bussiness
{
    public class ClsTblDm_LoaimonBLL : ClsBindingBLL
    {
        private int _id;
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        ClsTblDm_LoaimonDAL ClsTblDm_Loaimondal = new ClsTblDm_LoaimonDAL();
        public bool FindSameName(int iBtn, string VarTen)
        {
            bool Value = false;
            DataSet ds;
            ds = ClsTblDm_Loaimondal.FindSameName(VarTen);
            switch (iBtn)
            {
                case 1:
                    {
                        if (ds.Tables[0].Rows.Count >= 1)
                        {
                            XtraMessageBox.Show("Không cho phép thêm trùng dữ liệu", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Question);
                            Value = false;
                        }
                        else
                        {

                            Value = true;
                        }
                        break;
                    }
                case 2:
                    {
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            return true;
                        }
                        DataRow Row = ds.Tables[0].Rows[0];

                        if (ds.Tables[0].Rows.Count >= 1 && this.Id != int.Parse(Row["Id"].ToString()))
                        {
                            XtraMessageBox.Show("Tên không cho phép trùng", "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Question);
                            Value = false;
                        }
                        else
                        {
                            Value = true;
                        }
                        break;
                    }
            }
            return Value;
        }
        public void FocusControl(TextEdit Textbox, GridView Grid)
        {
            Textbox.Focus();
            //Grid.Enabled = !Grid.Enabled;
        }
        public void BingSource_InputControl(TextEdit Textbox, GridView Grid)
        {
            if (Grid.RowCount > 0)
            {
                this.Id = int.Parse(Grid.GetFocusedRowCellValue("Id").ToString());
                Textbox.Text = Grid.GetFocusedRowCellValue("Tenloaimon").ToString();
            }
        }

        public void ClearControl(TextEdit TextBox)
        {
            TextBox.Text = "";

        }
        public void BingSource_DataGrid(GridControl GridControl)
        {
            GridControl.DataSource = this.ClsTblDm_Loaimondal.SelectAll();
        }

        public void Insert(string Ten)
        {
            ClsTblDm_Loaimondal.Insert(Ten);
        }
        public void Edit(int Id, string Ten)
        {
            ClsTblDm_Loaimondal.Edit(Id, Ten);
        }
        public void Del()
        {
            ClsTblDm_Loaimondal.Del(this.Id);
        }

    }
}
